package com.gw.booking.controller.backend;

import com.gw.booking.common.MessageCode;
import com.gw.booking.common.ResultObject;
import com.gw.booking.common.Util;
import com.gw.booking.dto.PageDto;
import com.gw.booking.dto.backend.*;
import com.gw.booking.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;

@Api(tags = {"后台-预约管理"})
@Controller
@RequestMapping("/backend/order")
public class BOrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "订单列表")
    @RequestMapping(value = "/list.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<PageDto<OrderListDtoOut>> list(HttpServletRequest request
            , @ApiParam(value = "商家名称") @RequestParam(required = false) String shopName
            , @ApiParam(value = "预约编号") @RequestParam(required = false) String orderCode
            , @ApiParam(value = "预约者姓名") @RequestParam(required = false) String name
            , @ApiParam(value = "预约状态:0预约确认中,1预约完成,2到店,3取消,4却下,5已超时", required = true, allowableValues = "0,1,2,3,4,5") @RequestParam Integer state
            , @ApiParam(value = "开始时间(yyyy-mm-dd)") @RequestParam(required = false) String beginDate
            , @ApiParam(value = "结束时间(yyyy-mm-dd)") @RequestParam(required = false) String endDate
            , @ApiParam(value = "店铺类型：0已绑1未绑", allowableValues = "0,1") @RequestParam(required = false) Integer wechat
            , @ApiParam(value = "店铺类型id") @RequestParam(required = false) Integer merchantTypeId
            , @ApiParam(value = "页数", required = true) @RequestParam int page
            , @ApiParam(value = "数量", required = true) @RequestParam int size)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException {
        return new ResultObject(MessageCode.CODE_SUCCESS, orderService.list(shopName, orderCode, name, state, beginDate, endDate, wechat, merchantTypeId, page, size, Util.getLoginToken(request)));
    }

    @ApiOperation(value = "预约详情")
    @RequestMapping(value = "/detial.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<OrderDetailDtoOut> detial(HttpServletRequest request
            , @ApiParam(value = "订单id") @RequestParam(required = false) Integer id)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, orderService.detail(id, Util.getLoginToken(request)));
    }

    @ApiOperation(value = "预约编辑")
    @RequestMapping(value = "/update.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject update(HttpServletRequest request
            , @Valid @RequestBody OrderDetailDto orderDetailDto
            , BindingResult errors)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        orderService.txUpdate(Util.getLoginToken(request), orderDetailDto);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }
}
